<?php

/**
 * 获取数组val值中的字段
 * @param Array $arr
 * @param string $field
 * return Array
 */
function getArrayByField(&$arr,$field, $key = '') {
	$aRet = array();
	if ($key !== '') {
		foreach ($arr AS $aVal) {
			$aRet[$aVal[$key]] = $aVal[$field];
		}
	} else {
		foreach ($arr AS $aVal) {
			$aRet[] = $aVal[$field];
		}
	}
	return $aRet;
}

/**
 * 根据Val值，重新排序数组
 * @param Array $arr
 * @param unknown_type $k
 */
function setArrayKey(&$arr,$k) {
	$aRet = array();
	foreach ($arr AS $key=>$val) {
		$aRet[$val[$k]][] = $arr[$key];
	}
	return $aRet;
}

/*
 * 获取request变量
 */
function getRequest($name, $default = '') {
	if (isset($_POST[$name])) {
		return $_POST[$name];
	} elseif (isset($_GET[$name])) {
		return $_GET[$name];
	} else {
		return $default;
	}
}


/*
 * 过滤逗号分割的数字字符串 如 1,2,3
 */
function filterNumList($numlist, $separator = ',') {
	$aNums = explode($separator, $numlist);
	
	foreach ($aNums as &$value) {
		$value = intval($value);
	}
	
	return join($separator, $aNums);
}



/*
 * 获取文件的URL访问地址
 * @param $fid file表的id
 * @param $thumb 缩略图标志，默认空返回原图（原文件）
 * @param default 默认图片
 */
function getFileUrl($fid, $thumb='', $default = '') {
	$File = D('File');
	$aFile = $File->getById($fid);
	$url = $File->getUrl($aFile, $thumb, $default);
	
	return $url;
}

/*
 * 替换{var}为$data['var']
 */
function _replaceValue(&$mVal, $data) {
	if (is_array($mVal)) {
		foreach ($mVal as &$val) {
			_replaceValue($val, $data);
		}
	} else {
		preg_match_all("/\{\w+\}/", $mVal, $matches);
		foreach ($matches[0] as $value) {
			$key = str_replace(array('{', '}'), '', $value);
			$mVal = str_replace($value, $data[$key], $mVal);
		}
	}
}

/*
 * 获取行业信息
 */
function gethangye(){
	$hangye = D('hangye')->where('is_del = 0')->select();
	return $hangye;
}

/*
 * 动态获取地区信息
 */
function getarea($type = 'pro',$aid = '0'){
	if($type == 'pro'){
		$return = D('area')->where('pid = 0')->select();
	}
	if($type == 'city'){
		$return = D('area')->where('pid = '.$aid)->select();
	}
	if($type == 'area'){
		$return = D('area')->where('pid = '.$aid)->select();
	}
	return $return;
}

/*
 * 根据area id 获取名称
 */
function getareaname($aid){
	$return = D('area')->where('area_id = '.$aid)->find();
	return $return['title'];
}
function getxueli($eid){
	$return = D('education')->where('eid = '.$eid)->find();
	return $return['edu'];
}
function getedu(){
	$return = D('education')->select();
	return $return;
}
function getworktime(){
	$return = D('work_time')->select();
	return $return;
}
function format_xingbie($xingbie){
	switch($xingbie){
		case 1:$xingbie = '帅哥';break;
		case 2:$xingbie = '美女';break;
	}
	return $xingbie;
}

function format_age($shengri){
	$large = time()-strtotime($shengri);
	$age = floor($large/31536000);
	return $age;
}

function format_brand($brand){
	$brandarray = explode(',',$brand);
	foreach($brandarray as $k => $v){
		$brands[$k] = D('brand')->where('bid = '.$v)->find();
	}
	return $brands;
}

function format_menu($id){
	if($id == 1){
		$i = "是";
	}else{
		$i = "<font color='red'>否</font>";
	}
	return $i;
}

function format_sendcount($hiring_id){
	$map['hiring_id'] = $hiring_id;
	$map['is_del'] = 0;
	$result = D('send_resume')->where($map)->count();
	return $result;
}
function format_workplace($stores){
	$key = 0;
	$area[] = $stores['data'][0]['area'];
	foreach($stores['data'] as $k => $v){
		if(!in_array($v['area'],$area)){
			$area[count($area)] = $v['area'];
		}
	}
	foreach($area as $k => $v){
		foreach($stores['data'] as $a => $b){
			if($b['area']==$v){
				$store[$b['area']]['stores'][count($store[$b['area']]['stores'])]=$b;
			}
		}
	}
	foreach($store as $k => &$v){
		$v['area'] = getareaname($k);
	}
	return $store;
}

?>